package com.vueblog.controller.admin;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.vueblog.po.User;
import com.vueblog.service.TokenService;
import com.vueblog.service.UserService;
import com.vueblog.token.AdminToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private TokenService tokenService;

    @AdminToken
    @GetMapping("/getAdminInfo")
    public Object getAdminInfo(){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("user",userService.getAdminInfo());
        return jsonObject;
    }

    @AdminToken
    @PostMapping("/saveAdmin")
    public Object saveAdmin(@RequestBody User user){
        JSONObject jsonObject = new JSONObject();
        int i = userService.updateAdminUser(user);
        if (i>0){
            User u = userService.getAdminInfo();
            String token = tokenService.getToken(u);
            u.setPassword(null);
            jsonObject.put("token",token);
            jsonObject.put("user",u);
            jsonObject.put("status",200);
        }else {
            jsonObject.put("msg","发生未知错误");
        }
        return jsonObject;
    }
}
